﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 控件行编辑
{
    public partial class Form1 : Form
    {
        TextBox txtEdit = new TextBox();

        public Form1()
        {
            InitializeComponent();
            txtEdit.KeyDown += new KeyEventHandler(txtEdit_KeyDown);
            SetListBox();
            BindListBox();
        }

        //设置ListBox，高度为20
        private void SetListBox()
        {
            listBox1.DrawMode = DrawMode.OwnerDrawFixed;
            listBox1.ItemHeight = 20;
        }

        /// <summary>
        /// 绑定ListBox
        /// </summary>
        private void BindListBox()
        {
            //可更改为listBox1.Items.Add(msg);
            ListItem li = new ListItem("0", "选项一");
            listBox1.Items.Add(li);
            li = new ListItem("1", "选项二");
            listBox1.Items.Add(li);
            li = new ListItem("2", "选项三");
            listBox1.Items.Add(li);
            listBox1.Items.Add("");
        }

        /// <summary>
        /// 选择项类，用于ComboBox或者ListBox添加项
        /// 这个类可以省去
        /// </summary>
        public class ListItem
        {
            private string id = string.Empty;
            private string name = string.Empty;
            public ListItem(string sid, string sname)
            {
                id = sid;
                name = sname;
            }
            public override string ToString()
            {
                return this.name;
            }

            public string ID
            {
                get
                {
                    return this.id;
                }
                set
                {
                    this.id = value;
                }
            }

            public string Name
            {
                get
                {
                    return this.name;
                }
                set
                {
                    this.name = value;
                }
            }
        }

        /// <summary>
        /// KeyDown事件定义
        /// </summary>
        private void txtEdit_KeyDown(object sender, KeyEventArgs e)
        {
            //Enter键 更新项并隐藏编辑框
            if (e.KeyCode == Keys.Enter)
            {
                listBox1.Items[listBox1.SelectedIndex] = txtEdit.Text;
                txtEdit.Visible = false;
            }

            //Esc键 直接隐藏编辑框
            if (e.KeyCode == Keys.Escape)
                txtEdit.Visible = false;
        }
        /// <summary>
        /// 必须将DrawMode属性设为OwnerDrawFixed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
        {
            e.DrawBackground();
            e.DrawFocusRectangle();
            e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor), e.Bounds);
        }

        private void listBox1_Click(object sender, EventArgs e)
        {
            txtEdit.Visible = false;
        }

        private void listBox1_DoubleClick(object sender, EventArgs e)
        {
            int itemSelected = listBox1.SelectedIndex;
            string itemText = listBox1.Items[itemSelected].ToString();

            Rectangle rect = listBox1.GetItemRectangle(itemSelected);
            txtEdit.Parent = listBox1;
            txtEdit.Bounds = rect;
            txtEdit.Multiline = true;
            txtEdit.Visible = true;
            txtEdit.Text = itemText;
            txtEdit.Focus();
            txtEdit.SelectAll();
        }
    }
}
